<?php

namespace app\common\model;
use think\Model;

class Category extends Model
{
    public function add($data)
    {
        $cotegory = new Category($data);
//        dump($data);
// 过滤post数组中的非数据表字段数据
        $res = $cotegory->allowField(true)->save();
        if ($res)
        {
            return $res = ['valid'=>1,'message'=>'操作成功'];
        }else
        {
            return $res = ['valid'=>2,'message'=>'操作失败'];
        }
    }


    public function edit($id,$data)
    {

        $Category = new Category();
// 过滤post数组中的非数据表字段数据
        $res= $Category->allowField(true)->save($data,['id' => $id]);
        if ($res)
        {
            return $res = ['valid'=>1,'message'=>'操作成功'];
        }else
        {
            return $res = ['valid'=>2,'message'=>'操作失败'];
        }
    }
//*******************************************前台***************************************************************
    public function getSon($data,$id)
    {

        static $temp;
        foreach($data as $k =>$v)
        {
            if($v['pid'] ==$id)
            {
                $temp[] = $v['id'];
                $this ->getSon($data,$v['id']);
            }
        }
        return $temp;
    }
    public function getFatherData($cateData,$id)
    {
        static $temp = [];
        foreach($cateData as $k =>$v)
        {
            if ($id == $v['id'])
            {
                $temp[] = $v;
                $this->getFatherData($cateData, $v['pid']);
            }
        }
            return $temp;
    }
}
